package xin.nick;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author Nick
 * @since 2023/4/18/018
 */
public class MainImage {
    public static void main(String[] args) throws IOException {

        // 原本的图片
        String oldFile = "D:\\work-file\\0fbe-pull.png";
        BufferedImage bufferedImage = ImageIO.read(new File(oldFile));

        int alpha = 255;
        int removeRgb = 255;
        int diff = 190;
        // 遍历Y轴的像素
        for (int y = bufferedImage.getMinY(); y < bufferedImage.getHeight(); y++) {
            // 遍历X轴的像素
            for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) {
                int rgb = bufferedImage.getRGB(x, y);
                int R = (rgb & 0xff0000) >> 16;
                int G = (rgb & 0x00ff00) >> 8;
                int B = (rgb & 0x0000ff);
                if ((Math.abs(removeRgb - R) < diff) && (Math.abs(removeRgb - G) < diff) && (Math.abs(removeRgb - B) < diff)) {
                    rgb = ((alpha + 1) << 24) | (rgb & 0x00ffffff);
                }
                bufferedImage.setRGB(x, y, rgb);
            }
        }

        // 输出文件路径
        String outFilePath = "./out.png";
        ImageIO.write(bufferedImage, "png", new File(outFilePath));

    }
    
}
